package com.tjetc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tjetc.common.Result;
import com.tjetc.entity.Tribe;
import com.tjetc.entity.User;
import com.tjetc.entity.UserTribe;
import com.tjetc.service.ITribeService;
import com.tjetc.service.IUserTribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/tribe")
public class TribeController {
    @Autowired
    private ITribeService tribeService;
    @Autowired
    private IUserTribeService userTribeService;

    @RequestMapping("/list")
    public PageInfo<Tribe> list(@RequestParam(defaultValue = "1") Integer curPage,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam(defaultValue = "" ) String name){
        return  tribeService.lists(curPage,pageSize,name);

    }

    @RequestMapping("/getTribeList1")
    public List<Tribe> getTribeList1(Integer id,Integer uid){
        //  id  ==2 的时候记得加上查询出来我加入的部落
        System.out.println("id = " + id);
        if(id==1){
            return tribeService.list();
        }else{

            return tribeService.list2(uid);
        }

    }
    @RequestMapping("/listById")
    public Tribe listById(Integer id){
        return tribeService.getById(id);
    }

    @RequestMapping("/add")
    public Result<?> add(Tribe tribe, MultipartFile photo){
        tribe.setCount(0);
        System.out.println("tribe = " + tribe + ", photo = " + photo);

        boolean i =tribeService.add(tribe,photo);
        if (i){
            return Result.success("添加成功");
        }else{
            return Result.fail("添加失败");
        }
    }

    @RequestMapping("/update")
    public Result<?> update(Tribe tribe, MultipartFile photo, String[] paths){
        boolean i=tribeService.updates(tribe,photo,paths);
        if (i){
            return Result.success("修改成功");
        }else{
            return Result.fail("修改失败");
        }
    }

    @RequestMapping("/del")
    public Result<?> del(Integer id){
        System.out.println("id = " + id);
        QueryWrapper<UserTribe> qw=new QueryWrapper<>();
        qw.eq("tribe_id",id);
        List<UserTribe> list= userTribeService.list(qw);
        if (list.size()>0){
            return Result.fail("请先解散部落人员");

        }else{
            tribeService.removeById(id);
            return Result.success();
        }

    }


}
